<?php

/** Generic database functions for Dataxi
 *  This code is licensed under GPL2.
 *  See LICENSE or www.gnu.org for more details.
 *  @author Jyry Kuukkanen
 *
 *  $Id: dzdbgeneric.php,v 1.4 2005/03/12 17:06:52 jyry Exp $
 */

include_once("dzgeneric.php");
include_once("sosqlhi.php");
include_once("sodebug.php");


/** Returns database access string
 *  @var bool $Alias When set, returns db alias name instead of actual
 *      access string.
 *  Database access string is fetched through site.ini/app.ini/kone.ini
 */
function dzGetDbAccess($Alias = 0) {
    $dbalias = dzGetIniItem(DZIS_GENERAL, DZID_DBALIAS, "app");
    if ($Alias) {
        return $dbalias;
    } else {
        return dzGetIniItem($dbalias, DZID_DBACCESS, "kone");
    }; // else if
}; // dzGetDbAccess


/** Returns db alias name empty or sys db names
 *  Empty db alias name results default dbalias name and special "sys" name
 *  returns sys dbalias or default dbalias when no sysdb specified.
 *  @param string $DbAlias Database alias name
 *  @return string Modified database alias name, if necessary.
 */
function dzGetDbAliasName($DbAlias) {
    /* Check for sys db alias request */
    if ($DbAlias == DZDBA_SYS) {
        $DbAlias = dzGetIniItem(DZIS_GENERAL, DZID_SYSDBALIAS, "app");
    }; // if

    /* Alias name not specified? Get default */
    if (!$DbAlias) $DbAlias = dzGetIniItem(DZIS_GENERAL, DZID_DBALIAS, "app");

    return $DbAlias;
}; // dzGetDbAliasName


/** Returns PEAR:DB object with DSN and DB options for an db alias
 *  Returns associative array with four elements: DBalias name, DSN string,
 *  PEAR:DB object and possible DBoptions (an associative array)
 *  @param string $DbAlias Db alias to get.
 *  @return array/string Returns array(DZDBA_NAME => dbalias_name,
 *  DZDBA_DSN => dsn_string, DZDBA_OPTIONS => array(<options>))
 */
function dzGetDbAlias($DbAlias = "") {
    $DbAlias =  dzGetDbAliasName($DbAlias);

    $dsn = dzGetIniItem($DbAlias, DZDBA_DSN, "kone");
    $ini = dzGetIni("kone");
    if ($ini) {
        $optset = $ini->getValue($DbAlias, DZDBA_OPTIONS, NULL);
        if ($optset) {
            $options = $optset->getAsArray();
        };
    }; // if
    if (!$options) $options = array();
    $dtt_path = dzGetiniPath("ini")."/".$DbAlias.".dtt";
    if (file_exists($dtt_path)) $options[SODA_DBTT] = "file://".$dtt_path;
    $result = array(DZDBA_NAME => $DbAlias,
                    DZDBA_DSN => $dsn,
                    DZDBA_OPTIONS => $options,
                    DZDBA_OBJ => soDbConnect($dsn));

    return $result;
}; // dzGetDbAlias


/** For backward compatibility reasons only. See dzQuickFetch for more.
 *  @param string $Table Table name to fetch from
 *  @param array/string $KeyCols Key column names
 *  @param array/any $KeyVals Key column values
 *  @param array/string $Datacols Column names to fetch
 *  @return array/any Values to return (1st row only if any) or NULL when no
 *  rows found.
 */
function dzSimpleFetch($TableName, $KeyCols, $KeyVals, $DataCols) {
    $qresult = dzQuickFetch($TableName,
                            $DataCols,
                            soJoinKeyValueArrays($KeyCols, $KeyVals));
    $result = array();
    foreach ($qresult->getKeys() as $key) {
        $result[] = $qresult->getArrayItem($key, 0);
    }; // foreach
    if (!count($result)) $result = NULL;

    return $result;
}; // dzSimpleFetch


/** Quick fetch from one table
 *  NOTE! This will completely replace dzSimpleFetch shortly. Please get
 *  rid of all code that uses dzSimpleFetch.
 *  Fetches one or more cols from one table by one or more conditions (keys)
 *  than may only contain "col=value" pairs.
 *  Each column name may and should include datatype when not of type string.
 *  Datatype is appended to column name by ":", e.g. "colname:int" would
 *  specify column "colname" to be of type "int". See sosqlhi.php for all
 *  possible datatypes.
 *  @param string $Table Table name to fetch from
 *  @param array/string $Datacols Column names to fetch
 *  @param array/string $KeyCols Associative array of key column names and
 *      values. Array key contains the column name and the array value the key
 *      value: array("keycolname" => "value", "key2" => "value2")
 *  @param array/any $SortCols Associative array of sort column values and
 *      direction (up/down). Array key contains the column name and the array
 *      value either "", "asc" or 0 for accending sort, or other values for
 *      decending sort: array("upsortcol" => "asc", "upway" => 0, "down" => 1)
 *  @param string $Select Either SODA_SELECT or SODA_SELECT_DISTINCT
 *  @return soArraySet Values to return
 */
function dzQuickFetch($TableName, $DataCols,
                      $KeyCols = array(), $SortCols = array(),
                      $Select = SODA_SELECT) {
    $dbaccess = dzGetDbAlias();
    $soda = new soDa($dbaccess[DZDBA_OBJ], $dbaccess[DZDBA_OPTIONS]);
    $fetch = new soDbFetch($soda);

    $fetch->setTable($TableName);
    if (count($KeyCols)) {
        $fetch->setKeyCols($KeyCols);
    };
    if (count($SortCols)) {
        $fetch->setSortCols($SortCols);
    };
    $fetch->setCols($DataCols);
    $result = $fetch->run(1, $Select);

    soDbDisconnect($dbaccess[DZDBA_OBJ]);
    return $result;
}; // dzQuickFetch

?>
